package com.amos.knife4j.config.exception.service;

import com.amos.knife4j.config.exception.BusinessException;
import com.amos.knife4j.config.result.ResultEnums;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * service异常处理aop
 *
 * @author meng_lbo
 * @date 2018/12/21  9:18
 */
@Component
@Aspect
public class ServiceExceptionAspect {
    private static final Logger log = LoggerFactory.getLogger(ServiceExceptionAspect.class);

    public ServiceExceptionAspect() {
    }

    @AfterThrowing(
            pointcut = "@annotation(com.amos.knife4j.config.exception.service.ServiceExceptionHandler)",
            throwing = "throwable"
    )
    public void afterRuntimeException(JoinPoint joinPoint, Throwable throwable) throws BusinessException {
        //处理
        if (throwable instanceof BusinessException) {
            throw (BusinessException) throwable;
        } else if (throwable instanceof RuntimeException) {
            log.error("service:" + throwable.getMessage(), throwable);
            throw new BusinessException(ResultEnums.ERROR);
        }
    }
}